﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour {
	
	public GameObject btnContinue;


	public PikachuLevelSelect selectLevelPanel;
    public Transform gameModePanel;
    public GameObject mainPaneL;
    public GameObject btnPlay;


    public Text content_Text;

	void Start(){
        // Save.setTutorial(0);
		// Save.setTutorialStore(0);
		//btnContinue.SetActive(Save.canContinue());
        //btnPlay.SetActive(Save.canContinue());
        selectLevelPanel.gameObject.SetActive(false);
        selectLevelPanel.Init();
		// btnDebugPostScore.SetActive (GameConfig.DEBUG_KEY);
		// btnDebugResetHighScore.SetActive (GameConfig.DEBUG_KEY);
		// updateButtonEventUI ();
        Transform easyMode = gameModePanel.Find("content/btnNormal");
		Transform middleMode = gameModePanel.Find("content/btnMiddle");
		Transform hardMode = gameModePanel.Find("content/btnHard");
		Transform endlessMode = gameModePanel.Find("content/btnEndless");
        easyMode.GetComponent<Button>().onClick.AddListener(()=>{
            selectLevelPanel.GetComponent<PikachuLevelSelect>().showLevelPanel(1);
        });
        middleMode.GetComponent<Button>().onClick.AddListener(() =>
        {
            selectLevelPanel.GetComponent<PikachuLevelSelect>().showLevelPanel(2);
        });
        hardMode.GetComponent<Button>().onClick.AddListener(() =>
        {
            selectLevelPanel.GetComponent<PikachuLevelSelect>().showLevelPanel(3);
        });
        endlessMode.GetComponent<Button>().onClick.AddListener(() =>
        {
            if (Save.canContinueEvent())
            {
                showChooseModePopup();
            }
            else 
            {
                selectLevelPanel.GetComponent<PikachuLevelSelect>().loadingScene.loadLevelGamplayEvent();
            }
        });

        // 未保存弹出框
       
	}
	void Update() {
		if (Save.getTutorialStatus() == 0) {
			Tutorial.TUTORIAL_MODE = 0;
			SceneManager.LoadScene (StringUtils.scene_play_tutorial);
			//AdsManager.ins.hideBannerView ();
            selectLevelPanel.transform.parent.gameObject.SetActive(false);
		}
	}

    public void OpenShop()
    {
    }

    public void Back()
    {
        SceneManager.LoadScene(1);
    }

	public void StartOfflineMode(){
		SceneManager.LoadScene ("LevelSence");
	}
	public void StartOnlineMode(){
		SceneManager.LoadScene ("OnlinePlay");
	}
	public void StartTutorial() {
		Tutorial.TUTORIAL_MODE = 0;
		SceneManager.LoadScene ("Help");
	}

    public void QuitGame()
    {
#if !UNITY_IOS
        if (Save.getRateStatus() == Const.STATUS_RATE_REMIND && Save.canShowRate())
        {
            GameStatic.ratePopup.ShowPopup();
        }
        else
        {
            GameStatic.handleQuitApp();
        }
#endif
    }

    //public void showLeaderBoard() {
    //	if (GameStatic.IsConnectedGooglePlayService) {
    //		CustomDebug.Log ("show LeaderBoard");
    //		Social.ShowLeaderboardUI ();
    //	} else {
    //		ToastManager.showToast ("Unable connect to google play service, will connect again");
    //		GameStatic.ConnectToGooglePlayService ();
    //	}
    //}

    public void showChooseModePopup() {
        mainPaneL.SetActive(true);
        mainPaneL.transform.Find("content").GetComponent<Animator>().Play("showpause");
    }

	public void hideChoo2seModePopup() {
        // Invoke("hideParent", 0.2f);
        selectLevelPanel.GetComponent<Animator>().Play("hidepause");
        if(!Save.canContinue())
        SceneManager.LoadScene(StringUtils.scene_SelectGames);
	}

    void hideParent()
    {
        // selectLevelPanel.transform.parent.gameObject.SetActive(false);
    }

    //public void hideBannerView() {
    //    AdsManager.ins.hideBannerView();
    //}

	public void endAnimaMain() {
		//AdsManager.ins.showBannerView();
		updateButtonEventUI();
		//GameStatic.networkConnectionStatus.checkNetwork (result => {
		//	if (result) {
		//		int rand = UnityEngine.Random.Range(0, 100);
		//		if (rand < GameConfig.probability_ads_visible) {
		//			GameStatic.adsPopup.loadAds ();	
		//		}
		//	}	
		//});
	}

	public void updateButtonEventUI() {
		//bool hasEvent = GSM.instance.eventData == null ? false : true;
		//btnEvent.SetActive (hasEvent);
		//if (hasEvent) {
		//	int status = (int) GSM.instance.eventData.GetInt("status");
		//	string eventName = (string) GSM.instance.eventData.GetString("event_name");
		//	if (status == EventInfo.STATUS_PREPARE) {
		//		timeEventRamain.setText("Preparing Time");
		//	} else if (status == EventInfo.STATUS_ONGOING) {
		//		timeEventRamain.StartRun ((int)(GSM.instance.eventData.GetLong ("time") / 1000),done=>{
		//			GSM.instance.getEventData();
		//		});
		//	} else {
		//		timeEventRamain.setText("Event ended");
		//	}
		//}
		// btnContinueEvent.SetActive (hasEvent && Save.canContinueEvent ());
	}

	public void showEventMainUI() {
		//GameStatic.eventArea.show ();
	}

    public void showMoreApp()
    {
#if UNITY_ANDROID
        Application.OpenURL("");
#elif UNITY_IOS
        Application.OpenURL("");
#endif
    }

    //public void onBtnRank()
    //{
    //    RankManager.instance.showRank();
    //}
}
